package com.example.dao.implDao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.dao.NotificationDao;
import com.example.mapper.*;
import com.example.pojo.EnrollInfo;
import com.example.pojo.Notification;
import com.example.pojo.NotificationReadStatus;
import com.example.pojo.StuInfo;
import com.example.pojo.dto.NotificationDTO;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Repository
public class NotificationDaoImpl implements NotificationDao {
    @Resource
    private NotificationMapper notificationMapper;

    @Resource
    private FamilyMapper familyMapper;

    @Resource
    private TeacherMapper teacherMapper;

    @Resource
    private NotificationReadStatusMapper notificationReadStatusMapper;

    @Override
    public List<Notification> notificationList() {
        return notificationMapper.selectList(null);
    }

    @Override
    public String addOrUpdateNotification(Notification notification) {
        String result = "";
        if(Objects.isNull(notification.getNotificationId())) {
            notificationMapper.insert(notification);
            result="添加成功";
        }else{
            notificationMapper.updateById(notification);
            result="修改成功";
        }
        return result;
    }



    // 分页查询
    @Override
    public IPage<Notification> getNotificationInfoPage(int currentPage, int limit) {
        // 创建分页对象
        Page<Notification> page = new Page<>(currentPage, limit);
        return notificationMapper.selectPage(page,null);
    }


    @Override
    public IPage<Notification> getNotificationByTitleInfoPage(int currentPage, int limit, String title) {
        Page<Notification> page = new Page<>(currentPage,limit);
        QueryWrapper<Notification> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title",title);

        return notificationMapper.selectPage(page,queryWrapper);
    }

    @Override
    public String addNotificationForAllParents(Notification notification) {
        notificationMapper.insert(notification);
        if(notification.getNotificationType()==0){
            // 所有可见
            int i = parentStatuses(notification);
            if(i<0)
                return "添加失败";

            int i1 = teacherStatuses(notification);
            if(i1>0)
                return "添加成功";
            return "添加失败";
        }else if (notification.getNotificationType()==1){
            // 家长可见
            int i = parentStatuses(notification);
            if(i>0)
                return "添加成功";
            return "添加失败";
        }else{
            // 教师可见
            int i1 = teacherStatuses(notification);
            if(i1>0)
                return "添加成功";
            return "添加失败";
        }
    }

    public int parentStatuses(Notification notification){
        //获取所有家长的ID
        List<Long> allFamilyIds = familyMapper.selectAllParentIds();
        //为每个家庭创建通知阅读状态记录。设置为未读
        List<NotificationReadStatus> statuses = new ArrayList<>();
        for (Long userId : allFamilyIds) {
            NotificationReadStatus status = new NotificationReadStatus();
            status.setNotificationId(notification.getNotificationId());
            status.setUserId(userId);
            status.setIsRead(false); // 设置为未读
            statuses.add(status);
        }
        // 批量插入通知阅读状态记录
        return notificationReadStatusMapper.insertBatchSomeColumn(statuses);
    }

    public int teacherStatuses(Notification notification){
        // 获取所有教师的id
        List<Long> allTeacherIds = teacherMapper.selectAllTeacherIds();
        List<NotificationReadStatus> teacherStatuses = new ArrayList<>();
        for (Long userId : allTeacherIds) {
            NotificationReadStatus status = new NotificationReadStatus();
            status.setNotificationId(notification.getNotificationId());
            status.setUserId(userId);
            status.setIsRead(false); // 设置为未读
            teacherStatuses.add(status);
        }
        // 批量插入通知阅读状态记录
        return notificationReadStatusMapper.insertBatchSomeColumn(teacherStatuses);
    }

    @Override
    public List<NotificationDTO> getNotificationByUserId(Long userId) {
        return notificationReadStatusMapper.getNotificationByUserId(userId);
    }

    @Override
    public String isReadNotification(NotificationReadStatus notificationReadStatus) {
        System.out.println(notificationReadStatus.getIsRead());
        QueryWrapper<NotificationReadStatus> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",notificationReadStatus.getUserId()).eq("notification_id",notificationReadStatus.getNotificationId());
        int update = notificationReadStatusMapper.update(notificationReadStatus, queryWrapper);
        if(update>0){
            return "已阅";
        }
        return null;
    }

    @Override
    public List<NotificationDTO> getNotificationByUserIdAndType(Long userId, Integer notificationType) {
        return notificationReadStatusMapper.getNotificationByUserIdAndType(userId, notificationType);
    }

    @Override
    public List<NotificationDTO> getMyNotification(Long userId) {
        return notificationReadStatusMapper.getMyNotification(userId);
    }

    @Override
    public List<NotificationDTO> getNotReadNotificationByUserIdAndType(Long userId, Integer notificationType) {
        return notificationReadStatusMapper.getNotReadNotificationByUserIdAndType(userId, notificationType);
    }
}
